package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.CreativeProductRequestEntity;
import com.dinadi.yidi.ui.activity.ProductDetailsActivity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24
 * Time: 11:21
 */

public class ProductSubAllAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<CreativeProductRequestEntity> mDatas = new ArrayList<>();

    public void addAllData(List<CreativeProductRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public ProductSubAllAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView nick_name, nick_name_price, original_price;
        ImageView imageView;


        public ViewHolder(View itemView) {
            super(itemView);
            nick_name = (TextView) itemView.findViewById(R.id.nick_name);
            imageView = (ImageView) itemView.findViewById(R.id.img_product);
            nick_name_price = (TextView) itemView.findViewById(R.id.nick_name_price);
            original_price = (TextView) itemView.findViewById(R.id.original_price);
        }

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_creative_product, parent, false);
        return new ProductSubAllAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ProductSubAllAdapter.ViewHolder itemViewHolder = (ProductSubAllAdapter.ViewHolder) holder;
        itemViewHolder.nick_name.setText(mDatas.get(position).getTitle());
        String price = mDatas.get(position).getPrice();
        String originalPrice = mDatas.get(position).getOriginalPrice();
        itemViewHolder.nick_name_price.setText("￥" + price);
        if (price!=null && !price.equals(originalPrice)) {
            itemViewHolder.original_price.setText("￥" + originalPrice);
            itemViewHolder.original_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        }else {
            itemViewHolder.original_price.setText("");
        }
        String imgUrl = mDatas.get(position).getCoverImg();

        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.imageView);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_home_page, itemViewHolder.imageView);
            }
        } else {
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_home_page, itemViewHolder.imageView);

        }
        itemViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ProductDetailsActivity.class);
                intent.putExtra("productId", mDatas.get(position).getProductId());
                mContext.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

}
